<?php

include ("Graph.php");

class Gng {
	var $_graph;
	var $_patterns;

	public function printing (){
		$this->_graph->printing();
	}
	
	public function __construct (){
		srand(time());
		$this->_graph = new Graph();
		$this->_patterns = array();
		
		for($i = 0; $i < 100; $i++){
			$pattern = new Pattern("$i");
			for ($k = 0; $k < 5; $k++){
				$pattern->_pattern[] = rand (1,10);
			}
			$this->_patterns[] = $pattern;			
		}
		
		$dimension = count($this->_patterns[0]->_pattern);
		$n1 =  new Pattern();
		$n2 =  new Pattern();
		for ($i = 0; $i < $dimension; $i++) {
			$n1->_pattern[] = rand(1,100);
			$n2->_pattern[] = rand(1,100);
		}
		$this->_graph->add_node ($n1);
		$this->_graph->add_node ($n2);
	}
	
	public function run ($t, $eb, $en, $limit, $lb, $lf, $bt){
	    for ($k = 0; $k  < $t; $k++) {
			echo "Iteracion " . $k . '</br>'; 
	  		for ($i = 0; $i < count($this->_patterns); $i++){
				$this->_graph->clone_nodes();
				$n1 = $this->_graph->find_nearest($this->_patterns[$i]->_pattern);
				$n2 = $this->_graph->find_nearest($this->_patterns[$i]->_pattern);
				$this->_graph->increase_edges_ages($n1);
				$this->_graph->increase_local_error 
					($n1, sqrt($this->_patterns[$i]->euclidean_distance(
				$this->_graph->nodes[$n1]->pattern->_pattern)));
				$this->_graph->move_to($n1, $this->_patterns[$i]->_pattern, $eb, $en);

				if ($this->_graph->exists_edge($n1,$n2) ==  true) {
	  				$this->_graph->reset_edge($n1, $n2, 0);
	  			}else {
	  				$this->_graph->add_edge($n1, $n2, 0);
	  			}
	  			$this->_graph->remove_edges($limit);
	        
				if ($i % $lb == 0) {
	  				$max = $this->_graph->max_local_error();
	  				$smax = $this->_graph->max_local_error_from($max);
					$pmax = $this->_graph->__find_position_node($max);
					$psmax = $this->_graph->__find_position_node($smax);
					$val = $this->_graph->nodes[$pmax]->half_to($this->_graph->nodes[$psmax]->pattern->_pattern);
					$n3 = $this->_graph->add_node ($val);
					$this->_graph->erase_edge($max,$smax); 
					$this->_graph->add_edge($max,$n3,0.0);
					$this->_graph->add_edge($n3,$smax,0.0);
					$this->_graph->nodes[$max]->error *= $lf;
					$this->_graph->nodes[$smax]->error *= $lf;
					$this->_graph->nodes[$n3]->error = $this->_graph->nodes[$max]->error;
				}
	  			$this->_graph->descrease_local_error($bt);
	    	}
			$this->_graph->printing(); echo '</br>';
	    }
	}
}

/*
		
		$query = new query("select * from  features");
		foreach ($query->v as $reg){
			$pattern = new Pattern("$reg->titulo");
			$pattern->_pattern[] = ($reg->bitrate == '') ? 0 : $reg->bitrate;
			$pattern->_pattern[] = ($reg->vbitrate == '') ? 0 : $reg->vbitrate;
			$pattern->_pattern[] = ($reg->abitrate == '') ? 0 : $reg->abitrate;
			$pattern->_pattern[] = ($reg->framecount == '') ? 0 : $reg->framecount;
			$pattern->_pattern[] = ($reg->framerate == '') ? 0 : $reg->framerate;
			$pattern->_pattern[] = ($reg->framewidth == '') ? 0 : $reg->framewidth;
			$pattern->_pattern[] = ($reg->frameheight == '') ? 0 : $reg->frameheight;
			$pattern->_pattern[] = ($reg->audiochannels == '') ? 0 : $reg->audiochannels;
//			echo $pattern->_label . " ";
//			for ($i = 0; $i < count($pattern->_pattern); $i++)
//				echo $pattern->_pattern[$i] . " ";
//			echo "</br>";	

			$this->_patterns[] = $pattern; 
*/

?>
